

package com.bei_en.teacher.admin.controller.advertising;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bei_en.teacher.admin.model.advertising.bean.Advertising;
import com.bei_en.teacher.admin.model.advertising.condition.AdvertisingCondition;
import com.bei_en.teacher.admin.model.advertising.vo.AdvertisingVo;
import com.bei_en.teacher.admin.model.questions.vo.QuestionsVo;
import com.bei_en.teacher.admin.service.advertising.AdvertisingService;
import com.bei_en.teacher.admin.controller.BaseController;
import com.bei_en.common.web.JsonFailResult;
import com.bei_en.common.web.JsonResult;
import com.bei_en.common.web.JsonSuccessResult;
import com.mysql.fabric.xmlrpc.base.Array;
/**
 * 
 * @author Guocg
 * @date 2016-11-13 9:55:53
 */
@Controller
@RequestMapping("/api/Advertising")
public class AdvertisingAPPController extends BaseController {

    @Autowired
    AdvertisingService advertisingService;


    @RequestMapping("/advertising")
	@ResponseBody
    public JsonResult advertising(Model model,@Valid  AdvertisingCondition vo, BindingResult br, HttpSession session ) {
    	if (br.hasErrors())
       		return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());
    	
     	try {
     		
     	   List<AdvertisingVo> list =new ArrayList<AdvertisingVo>();
     	  vo.setQueryWhere("and ad_status!=1");
		   list = advertisingService.findAdvertisingPage(vo);
		   JSONArray array=new JSONArray();
		   JSONObject object=null;
		   AdvertisingVo advertisingVo=null;
		   for (int i = 0; i < list.size(); i++) {
			   object=new JSONObject();
			   advertisingVo=list.get(i);
	   seetingJsonObject(object,"adId",  advertisingVo.getAdId());
//	   seetingJsonObject(object,"adType",  advertisingVo.getAdType());
	   seetingJsonObject(object,"adTitle",  advertisingVo.getAdTitle());
//	   seetingJsonObject(object,"adSubhead",  advertisingVo.getAdSubhead());
	   seetingJsonObject(object,"adExcerpt",  advertisingVo.getAdExcerpt());
	   seetingJsonObject(object,"adPoster",  advertisingVo.getAdPoster());
	   seetingJsonObject(object,"adContent",  advertisingVo.getAdContent());
	   seetingJsonObject(object,"adTargetValue",  advertisingVo.getAdTargetValue());
//	   seetingJsonObject(object,"adLocation",  advertisingVo.getAdLocation());
//	   seetingJsonObject(object,"adSort",  advertisingVo.getAdSort());
//	   seetingJsonObject(object,"adViews",  advertisingVo.getAdViews());
//	   seetingJsonObject(object,"adStatus",  advertisingVo.getAdStatus());
	   seetingJsonObject(object,"adCreatedTime",  advertisingVo.getAdCreatedTime());
			   array.add(object);
		}
		return new JsonSuccessResult(session.getId(), array);
		
     	} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
    }
	 
    
    @RequestMapping("/advertisingAdd")
    @ResponseBody
    public JsonResult advertisingAdd(Model model,@Valid  AdvertisingCondition vo, BindingResult br, HttpSession session) {
      	if (br.hasErrors())
       		return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());
      	try {
      		
      	
		    AdvertisingVo advertising=new AdvertisingVo();
		    
	   advertising.setAdId(vo.getAdId());
	   advertising.setAdType(vo.getAdType());
	   advertising.setAdTitle(vo.getAdTitle());
	   advertising.setAdSubhead(vo.getAdSubhead());
	   advertising.setAdExcerpt(vo.getAdExcerpt());
	   advertising.setAdPoster(vo.getAdPoster());
	   advertising.setAdContent(vo.getAdContent());
	   advertising.setAdTargetValue(vo.getAdTargetValue());
	   advertising.setAdLocation(vo.getAdLocation());
	   advertising.setAdSort(vo.getAdSort());
	   advertising.setAdViews(vo.getAdViews());
	   advertising.setAdStatus(vo.getAdStatus());
	   advertising.setAdCreatedTime(vo.getAdCreatedTime());
		    
			advertisingService.insert(advertising);
			return new JsonSuccessResult(session.getId(), "成功");
      	} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
    }
    @RequestMapping("/advertisingEdit")
    public JsonResult advertisingEdit(Model model,@Valid  AdvertisingCondition vo, BindingResult br, HttpSession session) {
      	if (br.hasErrors())
       		return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());
      	
    	try {
    	AdvertisingVo advertising=new AdvertisingVo();
	
		advertisingService.updateSelective(advertising);
	
		return new JsonSuccessResult(session.getId(), "成功");
		
     	} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
    }

  
    
}
